package com.education.platform.controller;

import cn.dev33.satoken.annotation.SaCheckLogin;
import com.education.platform.common.Result;
import com.education.platform.dto.user.*;
import com.education.platform.service.UserService;
import com.education.platform.vo.user.UserIdentityVO;
import com.education.platform.vo.user.UserProfileVO;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

@Tag(name = "用户信息管理", description = "用户个人信息、资料管理等接口")
@RestController
@RequestMapping("/api/user")
@RequiredArgsConstructor
@SaCheckLogin
public class UserController {

    private final UserService userService;

    @Operation(summary = "获取个人信息")
    @GetMapping("/me")
    public Result<UserProfileVO> getMyProfile() {
        UserProfileVO profile = userService.getMyProfile();
        return Result.success(profile);
    }

    @Operation(summary = "更新个人资料")
    @PutMapping("/me")
    public Result<Void> updateMyProfile(@Validated @RequestBody UpdateProfileDTO updateProfileDTO) {
        userService.updateMyProfile(updateProfileDTO);
        return Result.success();
    }

    @Operation(summary = "上传头像")
    @PostMapping("/avatar")
    public Result<String> uploadAvatar(@RequestParam("file") MultipartFile file) {
        String avatarUrl = userService.uploadAvatar(file);
        return Result.success(avatarUrl);
    }

    @Operation(summary = "提交身份认证")
    @PostMapping("/identity/submit")
    public Result<Void> submitIdentity(@Validated @RequestBody SubmitIdentityDTO submitIdentityDTO) {
        userService.submitIdentity(submitIdentityDTO);
        return Result.success();
    }

    @Operation(summary = "获取身份认证状态")
    @GetMapping("/identity/status")
    public Result<UserIdentityVO> getIdentityStatus() {
        UserIdentityVO identityVO = userService.getIdentityStatus();
        return Result.success(identityVO);
    }

    @Operation(summary = "绑定手机号")
    @PostMapping("/bind-mobile")
    public Result<Void> bindMobile(@Validated @RequestBody BindMobileDTO bindMobileDTO) {
        userService.bindMobile(bindMobileDTO);
        return Result.success();
    }

    @Operation(summary = "获取用户统计数据")
    @GetMapping("/stats")
    public Result<com.education.platform.vo.user.UserStatsVO> getUserStats() {
        com.education.platform.vo.user.UserStatsVO stats = userService.getUserStats();
        return Result.success(stats);
    }
}
